package com.ymatou.iapi.m.couponservice.testcase;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.m.couponservice.parameter.GetSellerReceiveBatchesBean;
import com.ymatou.iapi.m.couponservice.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.m.couponservice.parameter.SellerReceiveBatchInfo;
import com.ymatou.iapi.m.couponservice.parameter.SellerReceiveBatches;
import com.ymatou.iapi.m.couponservice.service.GetSellerReceiveBatchesCall;
import com.ymatou.iapi.m.couponservice.service.SellerCreateBatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceMCallService;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
/**
*获取商家可以领取的批次列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetSellerReceiveBatches{
	private static GetSellerReceiveBatchesBean getsellerreceivebatchesBean;
	private static GetSellerReceiveBatchesCall getsellerreceivebatchesCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商家可以领取的批次列表");
	}

	@Before
	public void caseUp() {
		getsellerreceivebatchesBean=new GetSellerReceiveBatchesBean();
		getsellerreceivebatchesCall=new GetSellerReceiveBatchesCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getsellerreceivebatchesCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerReceiveBatches_001() {
		Logger.start(true,"获取商家可以领取的批次列表");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			getsellerreceivebatchesBean.setSellerId(sellercreatebatchBean.getSellerId());
			getsellerreceivebatchesBean.setEffectiveStartTime(sellercreatebatchBean.getEffectiveStartDate());
			getsellerreceivebatchesBean.setEffectiveEndTime(sellercreatebatchBean.getEffectiveEndDate());
			
			List<Map> result=new YmtCouponBatchWapper().selectSellerReceiveBatches(sellercreatebatchBean.getSellerId(),sellercreatebatchBean.getEffectiveStartDate(),sellercreatebatchBean.getEffectiveEndDate());
			
			getsellerreceivebatchesCall.setData(getsellerreceivebatchesBean);
			getsellerreceivebatchesCall.callService();	
			
			Logger.verifyEquals("0", getsellerreceivebatchesCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", getsellerreceivebatchesCall.getMessage(), "验证返回Message");

			SellerReceiveBatches infos = getsellerreceivebatchesCall.getBatchInfos();
			Logger.verifyEquals(result.size(), infos.getBatchInfos().size(), "验证无返回conversionBillId");

			List<SellerReceiveBatchInfo> sellerReceiveBatches = infos.getBatchInfos();
			for (int i=0;i<sellerReceiveBatches.size();i++)
			{
				SellerReceiveBatchInfo batch=sellerReceiveBatches.get(i);
				Logger.verifyEquals(Integer.parseInt(result.get(i).get("batchId").toString()), batch.getBatchId(), "验证返回BatchId");
				Logger.verifyEquals(result.get(i).get("batchCode").toString(), batch.getBatchCode(), "验证返回BatchCode");
				Logger.verifyEquals(result.get(i).get("batchName").toString(), batch.getBatchName(), "验证返回BatchName");
				  
				Logger.verifyEquals(result.get(i).get("EffectiveStartTime").toString().subSequence(0, 19),
						batch.getEffectiveStartTime().replace("T", " "),
		                     "验证返回getEffectiveStartTime与数据库中表Ymt_CouponSetting中dValidStart字段相同");

		        Logger.verifyEquals(result.get(i).get("EffectiveEndTime").toString().subSequence(0, 19),
		        		batch.getEffectiveEndTime().replace("T", " "),
		                     "验证返回getEffectiveEndTime与数据库中表Ymt_CouponSetting中dValidEnd字段相同");
		        Logger.verifyEquals(0,
		        		batch.getOrderMinAmount().compareTo(new BigDecimal(result.get(i).get("OrderMinAmount").toString())),
	                      "验证返回BatchCode与数据库中表中fMinOrderValue字段相同");
	            Logger.verifyEquals(0,batch.getCouponAmount().compareTo(new BigDecimal(result.get(i).get("CouponAmount").toString())),
	                "验证返回BatchCode与数据库中表中fMinOrderValue字段相同");  
	            
	            Logger.verifyEquals(sellercreatebatchBean.getCouponTotalCount(), batch.getCouponTotalCount(), "验证返回BatchId");
	            Logger.verifyEquals(0, batch.getReceivedCount(), "验证返回BatchId");
				
	            
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerReceiveBatches_002() {
		Logger.start(true,"获取商家可以领取的批次列表-sellerId空");
		try {

			getsellerreceivebatchesCall.setData(getsellerreceivebatchesBean);
			getsellerreceivebatchesCall.callService();	
			
			Logger.verifyEquals("0", getsellerreceivebatchesCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", getsellerreceivebatchesCall.getMessage(), "验证返回Message");

			SellerReceiveBatches infos = getsellerreceivebatchesCall.getBatchInfos();
			Logger.verifyEquals(0, infos.getBatchInfos().size(), "验证无返回");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerReceiveBatches_003() {
		Logger.start(true,"获取商家可以领取的批次列表-不在时间范围内的不会返回");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			Date dNow = new Date(); // 当前时间
		    Date dBefore = new Date();
		    Date dAfter = new Date();

		    Calendar calendar1 = Calendar.getInstance(); // 得到日历
		    calendar1.setTime(dNow);// 把当前时间赋给日历
		    calendar1.add(Calendar.DAY_OF_MONTH, -10); // 设置为前一天
		    dBefore = calendar1.getTime(); // 得到前一天的时间

		    Calendar calendar2 = Calendar.getInstance(); // 得到日历
		    calendar2.setTime(dNow);// 把当前时间赋给日历
		    calendar2.add(Calendar.DAY_OF_MONTH, -2); // 设置为后十天
		    dAfter = calendar2.getTime(); // 得到前一天的时间
		    
			getsellerreceivebatchesBean.setSellerId(sellercreatebatchBean.getSellerId());
			getsellerreceivebatchesBean.setEffectiveStartTime(dBefore);
			getsellerreceivebatchesBean.setEffectiveEndTime(dAfter);
			
			getsellerreceivebatchesCall.setData(getsellerreceivebatchesBean);
			getsellerreceivebatchesCall.callService();	
			
			Logger.verifyEquals("0", getsellerreceivebatchesCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", getsellerreceivebatchesCall.getMessage(), "验证返回Message");

			SellerReceiveBatches infos = getsellerreceivebatchesCall.getBatchInfos();
			Logger.verifyEquals(0, infos.getBatchInfos().size(), "验证无返回conversionBillId");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerReceiveBatches_004() {
		Logger.start(true,"获取商家可以领取的批次列表-未开启无门槛的不会返回");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchBean.setIsShowOnPage(false);
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			getsellerreceivebatchesBean.setSellerId(sellercreatebatchBean.getSellerId());
			getsellerreceivebatchesBean.setEffectiveStartTime(sellercreatebatchBean.getEffectiveStartDate());
			getsellerreceivebatchesBean.setEffectiveEndTime(sellercreatebatchBean.getEffectiveEndDate());
			
			getsellerreceivebatchesCall.setData(getsellerreceivebatchesBean);
			getsellerreceivebatchesCall.callService();	
			
			Logger.verifyEquals("0", getsellerreceivebatchesCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", getsellerreceivebatchesCall.getMessage(), "验证返回Message");

			SellerReceiveBatches infos = getsellerreceivebatchesCall.getBatchInfos();
			Logger.verifyEquals(0, infos.getBatchInfos().size(), "验证无返回conversionBillId");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerReceiveBatches_005() {
		Logger.start(true,"获取商家可以领取的批次列表-已作废的不会返回");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			couponServiceMCallService.InvalidSellerBatch(sellercreatebatchBean.getSellerId(), sellerCreateBatchCall.getBatchCode());
			
			getsellerreceivebatchesBean.setSellerId(sellercreatebatchBean.getSellerId());
			getsellerreceivebatchesBean.setEffectiveStartTime(sellercreatebatchBean.getEffectiveStartDate());
			getsellerreceivebatchesBean.setEffectiveEndTime(sellercreatebatchBean.getEffectiveEndDate());
			
			getsellerreceivebatchesCall.setData(getsellerreceivebatchesBean);
			getsellerreceivebatchesCall.callService();	
			
			Logger.verifyEquals("0", getsellerreceivebatchesCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", getsellerreceivebatchesCall.getMessage(), "验证返回Message");

			SellerReceiveBatches infos = getsellerreceivebatchesCall.getBatchInfos();
			Logger.verifyEquals(0, infos.getBatchInfos().size(), "验证无返回conversionBillId");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerReceiveBatches_006() {
		Logger.start(true,"获取商家可以领取的批次列表-已过期的不会返回");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
	
		    SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(sellerCreateBatchCall.getBatchCode()).get(0);

			Date dNow = new Date(); // 当前时间
		    Date dBefore = new Date();
		    Date dAfter = new Date();

		    Calendar calendar1 = Calendar.getInstance(); // 得到日历
		    calendar1.setTime(dNow);// 把当前时间赋给日历
		    calendar1.add(Calendar.DAY_OF_MONTH, -10); // 设置为前一天
		    dBefore = calendar1.getTime(); // 得到前一天的时间

		    Calendar calendar2 = Calendar.getInstance(); // 得到日历
		    calendar2.setTime(dNow);// 把当前时间赋给日历
		    calendar2.add(Calendar.DAY_OF_MONTH, -2); // 设置为后十天
		    dAfter = calendar2.getTime(); // 得到前一天的时间
		    
		    new YmtCouponSettingWapper().setdValidEnd(batch.getiCouponSettingId(), dAfter);
			getsellerreceivebatchesBean.setSellerId(sellercreatebatchBean.getSellerId());
			getsellerreceivebatchesBean.setEffectiveStartTime(sellercreatebatchBean.getEffectiveStartDate());
			getsellerreceivebatchesBean.setEffectiveEndTime(sellercreatebatchBean.getEffectiveEndDate());
			
			getsellerreceivebatchesCall.setData(getsellerreceivebatchesBean);
			getsellerreceivebatchesCall.callService();	
			
			Logger.verifyEquals("0", getsellerreceivebatchesCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", getsellerreceivebatchesCall.getMessage(), "验证返回Message");

			SellerReceiveBatches infos = getsellerreceivebatchesCall.getBatchInfos();
			Logger.verifyEquals(0, infos.getBatchInfos().size(), "验证无返回conversionBillId");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerReceiveBatches_007() {
		Logger.start(true,"获取商家可以领取的批次列表-验证couponTotalCount与receivedCount");
		try {
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			
			SellerCreateBatchBean sellercreatebatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			sellercreatebatchBean.setCouponTotalCount(40);
			SellerCreateBatchCall sellerCreateBatchCall = couponServiceMCallService.SellerCreateBatch(sellercreatebatchBean);
			
			couponServiceMCallService.receiveCouponInfo(sellerCreateBatchCall.getBatchCode(), 1234);
			
			getsellerreceivebatchesBean.setSellerId(sellercreatebatchBean.getSellerId());
			getsellerreceivebatchesBean.setEffectiveStartTime(sellercreatebatchBean.getEffectiveStartDate());
			getsellerreceivebatchesBean.setEffectiveEndTime(sellercreatebatchBean.getEffectiveEndDate());
			
			List<Map> result=new YmtCouponBatchWapper().selectSellerReceiveBatches(sellercreatebatchBean.getSellerId(),sellercreatebatchBean.getEffectiveStartDate(),sellercreatebatchBean.getEffectiveEndDate());
			
			getsellerreceivebatchesCall.setData(getsellerreceivebatchesBean);
			getsellerreceivebatchesCall.callService();	
			
			Logger.verifyEquals("0", getsellerreceivebatchesCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", getsellerreceivebatchesCall.getMessage(), "验证返回Message");

			SellerReceiveBatches infos = getsellerreceivebatchesCall.getBatchInfos();
			Logger.verifyEquals(result.size(), infos.getBatchInfos().size(), "验证无返回conversionBillId");

			List<SellerReceiveBatchInfo> sellerReceiveBatches = infos.getBatchInfos();
			for (int i=0;i<sellerReceiveBatches.size();i++)
			{
				SellerReceiveBatchInfo batch=sellerReceiveBatches.get(i);
				Logger.verifyEquals(Integer.parseInt(result.get(i).get("batchId").toString()), batch.getBatchId(), "验证返回BatchId");
				Logger.verifyEquals(result.get(i).get("batchCode").toString(), batch.getBatchCode(), "验证返回BatchCode");
				Logger.verifyEquals(result.get(i).get("batchName").toString(), batch.getBatchName(), "验证返回BatchName");
				  
				Logger.verifyEquals(result.get(i).get("EffectiveStartTime").toString().subSequence(0, 19),
						batch.getEffectiveStartTime().replace("T", " "),
		                     "验证返回getEffectiveStartTime与数据库中表Ymt_CouponSetting中dValidStart字段相同");

		        Logger.verifyEquals(result.get(i).get("EffectiveEndTime").toString().subSequence(0, 19),
		        		batch.getEffectiveEndTime().replace("T", " "),
		                     "验证返回getEffectiveEndTime与数据库中表Ymt_CouponSetting中dValidEnd字段相同");
		        Logger.verifyEquals(0,
		        		batch.getOrderMinAmount().compareTo(new BigDecimal(result.get(i).get("OrderMinAmount").toString())),
	                      "验证返回BatchCode与数据库中表中fMinOrderValue字段相同");
	            Logger.verifyEquals(0,batch.getCouponAmount().compareTo(new BigDecimal(result.get(i).get("CouponAmount").toString())),
	                "验证返回BatchCode与数据库中表中fMinOrderValue字段相同");  
	            
	            Logger.verifyEquals(sellercreatebatchBean.getCouponTotalCount(), batch.getCouponTotalCount(), "验证返回BatchId");
	            Logger.verifyEquals(1, batch.getReceivedCount(), "验证返回BatchId");
				
	            
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}